package com.wan.fragment


import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wan.R
import com.wan.activity.MainActivity
import com.wan.adapter.SystemTabVpAdapter
import com.wan.base.BaseBean
import com.wan.base.BaseHandler
import com.wan.base.WanApp
import com.wan.entity.SystemTabEntity
import com.wan.uitls.*
import kotlinx.android.synthetic.main.fragment_system.*
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory


/**
 * 体系
 * 一个viewPager 和一个TabLayout
 */
class SystemFragment : Fragment() {

    private val threadName: String = this.javaClass.name

    //数据
    private var itemList: ArrayList<SystemTabEntity> = ArrayList()
    //碎片
    private var fragmentList: ArrayList<SystemSonBaseFragment> = ArrayList()

    private lateinit var adapter: SystemTabVpAdapter


    private val handler = Handler {
        when (it!!.what) {
            CommonUtils.LOAD_FINISHED -> {
                adapter.notifyDataSetChanged()
                system_content_vp.offscreenPageLimit = itemList.size - 1
            }
        }

        true
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_system, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        adapter = SystemTabVpAdapter(childFragmentManager, itemList, fragmentList)
        system_tab_tl.setupWithViewPager(system_content_vp)
        system_content_vp.adapter = adapter
        initNetWork()
    }

    private fun initNetWork() {

        var apiManager = ApiManager(this)
        apiManager.getSystemTreeItemList().enqueue(object : Callback<BaseBean<ArrayList<SystemTabEntity>>> {
            override fun onFailure(call: Call<BaseBean<ArrayList<SystemTabEntity>>>, t: Throwable) {
                Log.e(threadName, "失败了 ==> " + t.message)
            }

            override fun onResponse(call: Call<BaseBean<ArrayList<SystemTabEntity>>>, response: Response<BaseBean<ArrayList<SystemTabEntity>>>) {
                Log.e(threadName, "成功了 ==> ${response.body()}")

                WanApp.threadPool.execute {
                    var dataList = response.body()!!.data
                    itemList.addAll(dataList)
                    for (i in 0 until dataList.size) {
                        var systemTabEntity = dataList[i]
                        var bundle = Bundle()
                        bundle.putString(SystemSonBaseFragment.DATA, systemTabEntity.toString())
                        Log.e(threadName, "获取到的数据==> ${systemTabEntity.toString()}")
                        fragmentList.add(SystemSonBaseFragment.newInstance(bundle))
                    }
                    handler.sendEmptyMessage(CommonUtils.LOAD_FINISHED)
                }

            }
        })
    }

    override fun onDestroy() {
        handler.removeCallbacksAndMessages(null)
        RetrofitHelper.get().cancelCall(this)
        super.onDestroy()
    }

}
